<?php
/**
 * Created by PhpStorm.
 * User: LiuJiaJun
 * Date: 2018/12/25
 * Time: 13:59
 */

namespace api\storage\controller;


use api\storage\model\ContainersModel;
use api\storage\service\ContainerService;
use think\Request;

class ContainerController extends AppBaseController
{

    private $service;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->service = new ContainerService();
    }

    /**
     * @api {GET} /api/storage/container 查询容器物料
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  index
     * @apiGroup  container
     * @apiVersion 0.0.1
     * @apiDescription 查找containerCode 里的相关数据
     *
     * @apiParam {string} containerCode 容器号
     *
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 请求成功
     * @apiSuccess {Array} data 物料信息
     * @apiError  Error
     **/
    public function index()
    {
        $containerCode = input('containerCode');
        $result = $this->service->queryContainerData($containerCode, $this->getStorageUuid());
        switch ($result['status']) {
            case -1:
                $this->error('容器号不存在');
                break;
            case 1:
                $this->success('请求成功', $this->convertResult($result['data']));
                break;
            default:
                $this->error('请求错误');
        }
    }

    /**
     * @api {GET} /api/storage/container/seat_check 3.入库检查站位
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  checkSeat
     * @apiGroup  container
     * @apiVersion 0.0.1
     * @apiDescription 绑定站位容器号前，先检查容器号和站位号
     *
     * @apiParam {string} seatCode 站位号
     * @apiParam {string} containerCode 容器号
     *
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 请求成功
     * @apiSuccess {Array} data 容器信息
     * @apiError  Error
     **/
    public function checkSeat()
    {
        $seatCode = input('seatCode');
        $result = $this->service->checkSeatCode($seatCode, $this->getStorageUuid());
        switch ($result['status']) {
            case -2:
                $this->error('站位号不存在');
                break;
            case -1:
                $this->error('站位不属于C类货架');
                break;
            case 1:
                $this->success('请求成功', $result['data']);
                break;
            default:
                $this->error('请求错误');
        }
    }

    /**
     * @api {GET} /api/storage/container/container_check 1.检查容器号
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  checkContainer
     * @apiGroup  container
     * @apiVersion 0.0.1
     * @apiDescription 检查容器号
     *
     * @apiParam {string} containerCode 容器号
     *
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 容器号存在
     * @apiError  Error
     **/
    public function checkContainer()
    {
        $containerCode = input('containerCode');
        $containerModel = new ContainersModel();
        $container = $containerModel->findByContainerCode($containerCode);
        if (empty($container) || empty($container->getData())) {
            $this->error('容器号不存在');
        } else {
            $this->success('容器号存在');
        }
    }

    /**
     * @api {POST} /api/storage/container 2.容器装箱
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  save
     * @apiGroup  container
     * @apiVersion 0.0.1
     * @apiDescription 装箱
     *
     * @apiParam {string} containerCode 容器号
     * @apiParam {Array} reelIds 数组类型的reelId
     *
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 装箱成功
     * @apiError  Error
     **/
    public function save()
    {
        $containerCode = strtoupper(input('containerCode'));
        $reelIds = array_unique($this->jsonDecode($_POST['reelIds']));
        if (empty($reelIds)) {
            $this->error('reelId为空');
        }
        $result = $this->service->putReelIdIntoContainer($containerCode, $reelIds, $this->getStorageUuid(), $this->getNickname());
        switch ($result['status']) {
            case 0:
                $this->error('装箱失败');
                break;
            case 1:
                $this->success('装箱成功');
                break;
            case 2:
                $this->error('该物料不存于入库单');
                break;
            default:
                $this->error('请求失败');
        }
    }

    /**
     * @api {PUT} /api/storage/container/seat_bind 4.容器绑定
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  bindSeat
     * @apiGroup  container
     * @apiVersion 0.0.1
     * @apiDescription 容器绑定并且上架
     *
     * @apiParam {string} seatCode 站位号
     * @apiParam {string} containerCode 容器号
     *
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 装箱成功
     * @apiError  Error
     **/
    public function bindSeat()
    {
        $seatCode = input('seatCode');
        $containerCode = input('containerCode');
        $result = $this->service->bindSeatWithContainer($seatCode, $containerCode, $this->getStorageUuid(), $this->getNickname());
        switch ($result['status']) {
            case 0:
                $this->error('绑定失败');
                break;
            case 1:
                $this->success('绑定成功');
                break;
            default:
                $this->error('请求失败');
        }
    }


}